package com.example.excelapache_poi.controller;

import com.alibaba.fastjson2.JSON;
import com.example.excelapache_poi.common.Result;
import com.example.excelapache_poi.common.ResultEnum;
import com.example.excelapache_poi.dto.UserDto;
import com.example.excelapache_poi.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by IntelliJ IDEA2022.3.2
 * @Author: Tenghw
 * @Date: 2023/07/03  22:35
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("/poi")
public class UserController extends BaseController{
    @Autowired
    UserService userService;

    /**
     * 上传/导入
     * @param file
     * @return responseEntity
     */
    @PostMapping(value = "/uploadExcel", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity<Object> uploadExcel(@RequestParam("file") MultipartFile file, HttpServletRequest request){
        try {
            System.out.println(UserController.class.getResource("").getPath());
            return userService.uploadExcel(file);

        }catch (Exception e){
            log.error("Route=>UserController method=>uploadExcel excel导入异常:{}",e.getMessage());
        }
        return null;
    }
    /**
     * 下载/导出
     * @param response
     * @param userDto
     * @return
     */
    @GetMapping (value = "downLoadExcel")
    public void downLoadExcel(HttpServletResponse response, @RequestBody @Validated UserDto userDto){
        try {
            log.info("Route=>UserController method=>downLoadExcel 请求参数:{}", JSON.toJSONString(userDto));
            userService.downLoadExcel(response,userDto);
            log.info("Route=>UserController method=>downLoadExcel excel导出成功！");
        }catch (Exception e){
            log.error("Route=>UserController method=>downLoadExcel excel导出异常:{}",e.getMessage());
        }
    }

    @ExceptionHandler({MethodArgumentNotValidException.class})
    @ResponseStatus(code= HttpStatus.BAD_REQUEST)
    public Result<Object> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
        log.error("Route=>UserController method=>handleException Exception");
        BindingResult bindingResult = ex.getBindingResult();
        StringBuilder sb = new StringBuilder("校验失败:");
        for (FieldError fieldError : bindingResult.getFieldErrors()) {
            sb.append(fieldError.getField()).append("：").append(fieldError.getDefaultMessage()).append(", ");
        }
        return Result.fail(ResultEnum.EXCEPTION_REQUEST.getCode(),ResultEnum.EXCEPTION_REQUEST.getMsg(),sb.toString());
    }
}
